package luogu.bfs;
//P1746 离开中山路

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2022/12/24 14:51
 */
public class LeaveZhongshanRoad {
    static class Pall{
        int x;
        int y;
        int l;

        public Pall(int x, int y, int l) {
            this.x = x;
            this.y = y;
            this.l = l;
        }
    }
    static int x2,y2,n;
    static boolean[][]st=new boolean[1010][1010];
    static char[][]arr=new char[1010][1010];
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        sc.nextLine();
        for (int i = 1; i <= n; i++) {
            String s=sc.next();
            arr[i] = s.toCharArray();
        }
        int x=sc.nextInt();
        int y=sc.nextInt();
        x2=sc.nextInt();
        y2=sc.nextInt();
        System.out.println(bfs(x, y));
    }
    public static int bfs(int x,int y){
        Queue<Pall> queue=new ArrayDeque<>();
        queue.add(new Pall(x,y,0));
        int[]dx={0,0,1,-1};
        int[]dy={1,-1,0,0};
        while (!queue.isEmpty()){
            Pall remove = queue.remove();
            if(remove.x==x2&&remove.y==y2){
                return remove.l;
            }
            for (int i = 0; i < 4; i++) {
                int a=remove.x+dx[i],b=remove.y+dy[i];
                if(a>=1&&b>=1&&a<=n&&b<=n&&!st[a][b]&&arr[a][b-1]=='0'){
                    st[a][b]=true;
                    queue.add(new Pall(a,b,remove.l+1));
                }
            }
        }
        return -1;
    }
}
